<?php

namespace Modules\AdminPermission\Http\Controllers;


use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Modules\AdminPermission\Services\PackageService;

class PackageController extends AdminPermissionController
{
    /**
     * 保存
     */
    public function store(Request $request)
    {
        $params = $request->only([
            'name',
            'description',
            'expired_at',
            'status',
            'permission_ids',
        ]);

        $this->validateWithArray($params, [
            'name' => 'required|string',
            'description' => 'required|string',
            'permission_ids' => 'nullable|json',
            'expired_at' => 'nullable|date',
            'status' => 'nullable|int|in:0,1',
        ]);
        if ($params['permission_ids']) {
            $params['permission_ids'] = json_decode($params['permission_ids'], true);
        } else {
            $params['permission_ids'] = [];
        }

        app(PackageService::class)->store($params);

        return $this->noContent();
    }

    /**
     * 更新
     */
    public function update(Request $request, int $id)
    {
        $params = $request->only([
            'name',
            'description',
            'expired_at',
            'status',
            'permission_ids',
        ]);

        $this->validateWithArray($params, [
            'name' => 'required|string',
            'description' => 'required|string',
            'permission_ids' => 'nullable|json',
            'expired_at' => 'nullable|date',
            'status' => 'nullable|int|in:0,1',
        ]);
        if ($params['permission_ids']) {
            $params['permission_ids'] = json_decode($params['permission_ids'], true);
        } else {
            $params['permission_ids'] = [];
        }

        app(PackageService::class)->update($id, $params);

        return $this->noContent();
    }

    public function index(Request $request)
    {
        $params = $request->only([
            'name',
            'description',
            'status',
            'start_expired_at',
            'end_expired_at',
            'page_size',
        ]);

        $this->validateWithArray($params, [
            'name' => 'nullable|string',
            'description' => 'nullable|string',
            'start_expired_at' => 'nullable|date',
            'end_expired_at' => 'nullable|date',
            'status' => 'nullable|int|in:0,1',
        ]);

        $builder = app(PackageService::class)->getBuilder($params);

        $pageSize = intval($params['page_size'] ?? 10);

        $items = $builder->paginate($pageSize);

        return $this->responsePaginate($items);
    }


    /**
     * Show the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function show($id)
    {
        $item = app(PackageService::class)
            ->getBuilder()
            ->where('id', $id)
            ->first();
        if (! $item) {
            return $this->noContent();
        }

        return $this->responseItem($item);
    }

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Renderable
     */
    public function destroy($id)
    {
        app(PackageService::class)->destroy($id);

        return $this->noContent();
    }

}
